package com.hao.hospital.service;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.hao.hospital.core.param.GlobalMap;
import com.hao.hospital.model.HospitalPerson;
import com.hao.hospital.model.RegisterRecord;
import com.hao.hospital.model.User;
import com.hao.hospital.util.GeneralMethod;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Record;
import com.rtmap.utils.http.RestfulTemplate;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;
import cn.jlook.jfinal.ext3.kit.SqlBuilder.JOIN;

public class MainService extends AbstractService {
	private static MainService me;

	public static MainService me() {
		if (null == me) {
			me = Enhancer.enhance(MainService.class);
		}
		return me;
	}

	/**
	 * 短信接口
	 */
	public String sendShortMessage( int user_id, String time, int person_id) {
		Record record= SqlBuilder.from(RegisterRecord.table).alias("r")
				.join(JOIN.LEFT, User.table, "u", "u.user_id=r.user_id")
				.join(JOIN.LEFT, HospitalPerson.table, "h", "h.person_id=r.person_id")
				.field("*")
				.where("u.user_id=? and h.person_id=?").find(user_id, person_id);
		String phoneNumber=record.getLong("phone").toString();
		if (StringUtils.isEmpty(phoneNumber)) {
			return "0";
			// 空号码
		} else {
			if (GeneralMethod.isMobileNO(phoneNumber) == true) {
				
				String content = GlobalMap.configMap.get("ContextThree").toString();
				content = String.format(content, record.getStr("user_name"), time, record.getStr("person_name"));
				String sendMessageAPI = GlobalMap.configMap.get("sendMessageAPI").toString();
				sendMessageAPI = String.format(sendMessageAPI, phoneNumber, content);
				String result = RestfulTemplate.INSTANCE.getForObject(sendMessageAPI, String.class);
				JSONObject jresult = JSONObject.parseObject(result);
				return jresult.get("rtnmsg").toString();
			} else {
				return "3";
			}
		}

	}
}
